{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "列表如何转化为ndarray,ndarray如何转化为列表",
   "id": "fd2de61724dfd56d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T13:01:55.056110Z",
     "start_time": "2025-02-12T13:01:54.811525Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列表转化为ndarray\n",
    "import numpy as np\n",
    "list1 = [1, 2, 3, 4]\n",
    "print(list1)\n",
    "print(type(list1))\n",
    "oneArray = np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)"
   ],
   "id": "1a77ae2c31d650c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 3.2 ndarray与Python原生list运算效率对比",
   "id": "3a1b18162952faad"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T13:07:35.424765Z",
     "start_time": "2025-02-12T13:07:35.144298Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "a = []\n",
    "# 随机一亿个数字\n",
    "for i in range(1000000000):\n",
    "    a.append(random.random())\n",
    "# ndarray计算效率优于python的原生list\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2- t1, t5- t4)"
   ],
   "id": "511b80991b4573cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0 0.0010030269622802734\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "4.1创建一维数组",
   "id": "518baf9d621bcb56"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T13:13:25.279973Z",
     "start_time": "2025-02-12T13:13:25.263980Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1=[1,2,3,4]\n",
    "oneArray=np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)\n",
    "#创建数组的多种形式\n",
    "#1.直接传入列表的方式\n",
    "t1=np.array([1,2,3])\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "#2.传入range生成序列\n",
    "t2=np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "'''\n",
    " [0123456789]\n",
    " <class'numpy.ndarray'>\n",
    "'''\n",
    "#3.使用numpy自带的np.arange()生成数组\n",
    "t3=np.arange(0,10,2)\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "id": "6d6a7266fe20f2ed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 4.2创建二维数组",
   "id": "64b6003574adcbf0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T13:16:38.135788Z",
     "start_time": "2025-02-12T13:16:38.100456Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2=[[1,2],[3,4],[5,6]]\n",
    "twoArray=np.array(list2) #将列表转化为二维数组\n",
    "print(twoArray)\n",
    "print(list2)\n",
    "\n",
    "twoArray.tolist() #将ndarray转化为列表"
   ],
   "id": "beeeab7921abccc3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4], [5, 6]]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "4.3常用属性",
   "id": "42bd590ce228331f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T13:20:48.418936Z",
     "start_time": "2025-02-12T13:20:48.388481Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2=[[1,2],[3,4],[5,6]]\n",
    "twoArray=np.array(list2)\n",
    "#获取数组的维度(注意：与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "#形状（行，列）\n",
    "print(twoArray.shape)\n",
    "#有多少个元素\n",
    "print(twoArray.size)\n",
    "#数据类型\n",
    "print(twoArray.dtype)"
   ],
   "id": "14e36f1403b66f38",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n",
      "int32\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "4.4调整数组的形状",
   "id": "da7e25320cea7910"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T00:52:45.801951Z",
     "start_time": "2025-02-14T00:52:45.773195Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "four=np.array([[1,2,3],[4,5,6]])\n",
    "#修改的是原有的\n",
    "four.shape=(3,2)\n",
    "print(four)\n",
    "#返回一个新的数组\n",
    "four=four.reshape(3,2)\n",
    "print(four)\n",
    "#将多维变成一维数组\n",
    "five=four.reshape((6,),order='C')\n",
    "#默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six=four.flatten(order='F') # 展开成一堆数组\n",
    "print(five)\n",
    "print('-' * 50)\n",
    "print(six)\n",
    "#拓展：数组的形状\n",
    "t=np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "#转换成二维\n",
    "t1=t.reshape((4,6))\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "print('-' * 50)\n",
    "#转换成三维\n",
    "# 最前面是轴零，轴越小，越靠外，最小的轴是最外层\n",
    "# 0轴，1轴，2轴\n",
    "t2 = t.reshape((2,3,4))\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(t2.ndim)\n",
    "# 0轴，1轴，2轴，3轴\n",
    "print('-' * 50)\n",
    "t3 = t.reshape((2, 3, 2, 2))\n",
    "print(t3)\n",
    "print(t3.shape)\n",
    "print(t3.ndim)"
   ],
   "id": "dc0a84ed432a68c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 2 3 4 5 6]\n",
      "--------------------------------------------------\n",
      "[1 3 5 2 4 6]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "3\n",
      "--------------------------------------------------\n",
      "[[[[ 0  1]\n",
      "   [ 2  3]]\n",
      "\n",
      "  [[ 4  5]\n",
      "   [ 6  7]]\n",
      "\n",
      "  [[ 8  9]\n",
      "   [10 11]]]\n",
      "\n",
      "\n",
      " [[[12 13]\n",
      "   [14 15]]\n",
      "\n",
      "  [[16 17]\n",
      "   [18 19]]\n",
      "\n",
      "  [[20 21]\n",
      "   [22 23]]]]\n",
      "(2, 3, 2, 2)\n",
      "4\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "7da7d7b589ab0b9c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T00:41:04.130269Z",
     "start_time": "2025-02-14T00:41:04.113224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "severn = five.reshape(3, 2) # 改变形状\n",
    "severn"
   ],
   "id": "3c2ba7a058675673",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 4.5将数组转成list",
   "id": "68ac88e41dc92e79"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T01:00:33.624480Z",
     "start_time": "2025-02-14T01:00:33.598225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将数组转成list\n",
    "a=np.array([9,12,88,14,25])\n",
    "list_a=a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "10e6e3e68d8c2dd5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 5、NumPy的数据类型",
   "id": "fbdc69724b87651e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:51:29.444219Z",
     "start_time": "2025-02-14T02:51:29.427245Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "#返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f=np.array([1,2,3,4,127],dtype=np.int8)\n",
    "print(f.itemsize)#1np.int8(一个字节)\n",
    "#获取数据类型\n",
    "print(f.dtype)\n"
   ],
   "id": "43212cc3a2769dab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "int8\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:51:31.772743Z",
     "start_time": "2025-02-14T02:51:31.753837Z"
    }
   },
   "cell_type": "code",
   "source": "f[4] = f[4] + 1 # 超出范围，报错，溢出",
   "id": "45a207ed69c682d3",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:51:32.634321Z",
     "start_time": "2025-02-14T02:51:32.616573Z"
    }
   },
   "cell_type": "code",
   "source": "f[4]",
   "id": "ddad8317131c740b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-128"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:53:42.496565Z",
     "start_time": "2025-02-14T02:53:42.482814Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#调整数据类型\n",
    "f1=f.astype(np.int64)\n",
    "print(f1.dtype)"
   ],
   "id": "6219b0c00344b5e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:54:22.754872Z",
     "start_time": "2025-02-14T02:54:22.731880Z"
    }
   },
   "cell_type": "code",
   "source": "random.random() # 0-1之间的随机小数",
   "id": "fbcd3e3749a70c88",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5527301215412377"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:56:21.750190Z",
     "start_time": "2025-02-14T02:56:21.727849Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#拓展随机生成小数\n",
    "#使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "arr=np.array([random.random()for i in range(10)])\n",
    "print(arr)\n",
    "print(arr.dtype)\n",
    "print(arr.itemsize)\n",
    "#取小数点后两位\n",
    "print(np.round(arr,2)) # 取arr小数点后两位小数，四舍五入"
   ],
   "id": "3734b0a8d8d2605e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.94\n",
      "[0.6212618  0.77763043 0.03490404 0.67048479 0.92140323 0.00210757\n",
      " 0.16600184 0.08046677 0.08630557 0.58251495]\n",
      "float64\n",
      "8\n",
      "[0.62 0.78 0.03 0.67 0.92 0.   0.17 0.08 0.09 0.58]\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 6数组的计算",
   "id": "72825b22d4760991"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "6.1数组和数的计算",
   "id": "326b77c26e936bed"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T02:59:33.446517Z",
     "start_time": "2025-02-14T02:59:33.428898Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=np.arange(24).reshape((6,4))\n",
    "print(t1)\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "7755635d502e00ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:02:26.436888Z",
     "start_time": "2025-02-14T03:02:26.414947Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=np.arange(24).reshape((6,4))\n",
    "t2=np.arange(100,124).reshape((6,4))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print('-' * 50)\n",
    "print(t1+t2)\n",
    "print('-' * 50)\n",
    "print(t1*t2)"
   ],
   "id": "8d7bec836e560299",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "--------------------------------------------------\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------------------------------------\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:03:22.708632Z",
     "start_time": "2025-02-14T03:03:22.522837Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=np.arange(24).reshape((4,6))\n",
    "t2=np.arange(18).reshape((3,6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1-t2)"
   ],
   "id": "21c9f985613a7ee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,6) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[38], line 5\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43mt2\u001B[49m)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (3,6) "
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:08:40.825063Z",
     "start_time": "2025-02-14T03:08:40.802466Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列相同，行减行，只限于另一维度为一维\n",
    "t1=np.arange(24).reshape((4,6))\n",
    "t2=np.arange(6).reshape((1,6))\n",
    "print(t1)\n",
    "print(t2) \n",
    "t1-t2 # 广播减"
   ],
   "id": "a954f7bbbcc07d8a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0,  0],\n",
       "       [ 6,  6,  6,  6,  6,  6],\n",
       "       [12, 12, 12, 12, 12, 12],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:08:16.066338Z",
     "start_time": "2025-02-14T03:08:16.052664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行相同，列减列，只限于另一维度为一维\n",
    "t1 =np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1-t2)"
   ],
   "id": "a0499cec655e209c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:13:15.153157Z",
     "start_time": "2025-02-14T03:13:15.128913Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.arange(24).reshape((2, 3, 4))\n",
    "b = np.arange(12).reshape((1, 3, 4))\n",
    "a - b"
   ],
   "id": "b777325ac66a21f6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  0,  0,  0],\n",
       "        [ 0,  0,  0,  0],\n",
       "        [ 0,  0,  0,  0]],\n",
       "\n",
       "       [[12, 12, 12, 12],\n",
       "        [12, 12, 12, 12],\n",
       "        [12, 12, 12, 12]]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "结论：ndim要相同，可以摸一个轴的size不同，但是其中一个ndarray必须对应的轴的size为1",
   "id": "7f5c8e80d2ac9cde"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "练习轴",
   "id": "217f6f48fc81ba09"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "1e74a8703cce1456"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:21:58.444523Z",
     "start_time": "2025-02-14T03:21:58.326552Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.sum(a,axis=0)) # [5 7 9]\n",
    "print(np.sum(a,axis = 1)) # [ 6 15]\n",
    "print(np.sum(a))\n",
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(d)"
   ],
   "id": "401627c9553117fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 7 数组的索引和切片",
   "id": "7ebd3ab2f8562f1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:24:10.211334Z",
     "start_time": "2025-02-14T03:24:10.182581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片\n",
    "# print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[0],a)\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "print(a[2:8:2])"
   ],
   "id": "ec938decf3ddeeb7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:47:43.478968Z",
     "start_time": "2025-02-14T03:47:43.441871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "print(t1)\n",
    "print('*' * 20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的)print(t1[1,:])\n",
    "print('*' * 20) \n",
    "print(t1[1:]) # 取连续多行 \n",
    "print('*' * 20) \n",
    "print(t1[1:3,:])  # 取连续多行，左闭右开\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3]]) # 取不连续多行"
   ],
   "id": "3e8098723df8270f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:38:29.534905Z",
     "start_time": "2025-02-14T03:38:29.441417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t2 = np.arange(24).reshape((4, 6)).tolist()\n",
    "t2[0, 2, 3]"
   ],
   "id": "1a419cbd790aaea4",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not tuple",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[53], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m t2 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m24\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape((\u001B[38;5;241m4\u001B[39m, \u001B[38;5;241m6\u001B[39m))\u001B[38;5;241m.\u001B[39mtolist()\n\u001B[1;32m----> 2\u001B[0m \u001B[43mt2\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: list indices must be integers or slices, not tuple"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:49:08.171387Z",
     "start_time": "2025-02-14T03:49:08.129329Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t1[:,1]) # 取一列\n",
    "print('*' * 20)\n",
    "print(t1[:,1:]) # 取连续多列\n",
    "print('*' * 20)\n",
    "print(t1[:, [0, 2, 3]]) # 取不连续多列\n",
    "print('*' * 20)\n",
    "print(t1[2, 3]) # 取一个元素\n",
    "print('*' * 20)\n",
    "print(t1[[0,1,1],[0,1,3]])\n",
    "t1[1:3,1:4] # 取1-3行， 1-4列"
   ],
   "id": "70eb60719cd08b46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[0 7 9]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 7,  8,  9],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "8、数组中的数值修改",
   "id": "e0ebdbd74bbb3899"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:12:29.783399Z",
     "start_time": "2025-02-14T06:12:29.672386Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    " # # 修改某一行的值\n",
    "t[1, :] = 0\n",
    " # # 修改某一列的值\n",
    "t[:, 1] = 0 \n",
    " # # 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "#  修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[0,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改，比如讲小于10的值改掉\n",
    "t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    " # np.logical_not\n",
    "t[(t > 2) & (t < 6)] = 0 # 与\n",
    "t[(t < 2) | (t > 6)] = 0 # 或\n",
    "t[~(t > 6)] = 0 # 非\n",
    "# print(t)\n",
    "t=t.clip(10,18)\n",
    "print(t)\n",
    " # # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )"
   ],
   "id": "e06eeef5c3cc1bbc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "三目运算符",
   "id": "ec4440b3364d1d25"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:14:57.357728Z",
     "start_time": "2025-02-14T06:14:57.312062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# python的三目运算符\n",
    "a = 10\n",
    "b = 15\n",
    "c=a if a>b else b\n",
    "b"
   ],
   "id": "8776829d9a74910d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 9 数组的添加、删除和去重",
   "id": "6325dcd077a6d1d9"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "9.1 数组的添加",
   "id": "96fe14527939e6a4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:25:03.637125Z",
     "start_time": "2025-02-14T06:25:03.536889Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. numpy.append 函数在数组的末尾添加值。 追加操作会分配整个数组，并把原来的数组复制到新数组中。\n",
    "# 此外，输入数组的维度必须匹配否则将生成ValueError。\n",
    "'''\n",
    "参 数 说明：\n",
    "arr： 输 入 数 组\n",
    "values：要向arr添加的值，需要和arr形状相同（除了要添加的轴）\n",
    "axis：默认为 None。当axis无定义时，是横向加成，返回总是为一维数组！当axis有定义的\n",
    "时候，分别为0和1的时候。当\n",
    "axis 有定义的时候，分别为0和1的时候（列数要相同）。当axis为1时，数组是加在右边\n",
    "（行数要相同）。\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('-' * 50)"
   ],
   "id": "5b8b9c55754612df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:27:18.222680Z",
     "start_time": "2025-02-14T06:27:18.193774Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.array([[7, 8, 9]])\n",
    "print(b.ndim)\n",
    "print(b)\n",
    "print('-' * 50)"
   ],
   "id": "887fc4f700ac14ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[[7 8 9]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:28:42.471871Z",
     "start_time": "2025-02-14T06:28:42.441135Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 0 添加元素：')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "print(a)\n",
    "print('-' * 50)"
   ],
   "id": "ef5bfdf5f8007791",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:35:58.386931Z",
     "start_time": "2025-02-14T06:35:58.359922Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 1, [11, 12], axis=0))\n",
    "print('\\n')\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "print('沿轴 0 广播：')\n",
    "print(np.insert(a, 1, [11], axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 广播：')\n",
    "print(np.insert(a, 1, [1, 2, 5], axis=1))"
   ],
   "id": "ecb6f3086456ae0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴 1 广播：\n",
      "[[1 1 2]\n",
      " [3 2 4]\n",
      " [5 5 6]]\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 9.2 数组中的删除",
   "id": "1dcb1b58c6ccef9d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:40:56.957340Z",
     "start_time": "2025-02-14T06:40:56.865501Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样，\n",
    "# 如果未提供轴参数， 则输入数组将展开。\n",
    "'''\n",
    "参 数 说明：arr： 输 入 数 组\n",
    "obj：可以被切片，整数或者整数数组，表明要从输入数组删除的子数组axis：\n",
    "沿着它删除给定子数组的轴，如果未提供，则输入数组会被展开\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a,5))\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('删除第一行：')\n",
    "print(np.delete(a,1,axis = 0))\n",
    "print('\\n')\n",
    "\n",
    "print('删除第一列：')\n",
    "print(np.delete(a, 1, axis=1))\n"
   ],
   "id": "6fe8e315fd5cabaa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "删除第一行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "删除第一列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 9.3 数组去重",
   "id": "4d3d60047a41ab75"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:48:57.023926Z",
     "start_time": "2025-02-14T06:48:56.942584Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.unique 函数用于去除数组中的重复元素。\n",
    "'''\n",
    " arr：输入数组，如果不是一维数组则会展开\n",
    "return_index：如果为 true，返回新列表元素在旧列表中的位置（下标），并以列表形式储\n",
    "return_inverse：如果为 true，返回旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "return_counts：如果为 true，返回去重数组中的元素在原数组中的出现次数\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "\n",
    "# 去除重复元素，输出的是有序序列\n",
    "# print ('第一个数组的去重值：')\n",
    "# u = np.unique(a)\n",
    "# print (u)\n",
    "# print ('\\n')\n",
    "\n",
    "print ('去重数组的索引数组：')\n",
    "u,indices = np.unique(a, return_index = True)\n",
    "print(u)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "\n",
    "# print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "# print (a)\n",
    "# print ('去重数组的下标：')\n",
    "# u,indices = np.unique(a,return_inverse = True)\n",
    "# print (u)\n",
    "# print (indices)\n",
    "# print ('\\n')\n",
    "\n",
    "print ('返回去重元素的重复数量：')\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u)\n",
    "print (indices)\n",
    "u"
   ],
   "id": "ddfdb19735fe014e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "\n",
      "去重数组的索引数组：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([2, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "10 numpy的计算（数学函数）",
   "id": "bf3088097f3dc33f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:02:41.487845Z",
     "start_time": "2025-02-14T07:02:41.346540Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=1)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n",
    "# 5. 数据的比较\n",
    "result = np.maximum( [-2,-1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.minimum( [-2,-1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "\n",
    "print(result)\n",
    "result = np.maximum( [-2,-1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "print(result)\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(0))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2 |3 |\n",
    "[4, 5, 6]------> |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(axis=1))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2+1 |3+2+1 |\n",
    "[4, 5, 6]------> |4 |4+5 |4+5+6 |\n",
    "'''\n",
    "# 8. argmin 求最小值索引\n",
    "result = np.argmin(score,axis=0)\n",
    "res=np.min(score,axis=0) #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "print(result,res)\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score,axis=0)\n",
    "print(result)\n",
    "# 10. 极 值\n",
    "result = np.ptp(score,axis=None)#就是最大值和最小值的差\n",
    "print(result)\n",
    "# 拓展：方差var, 协方差cov, 计算平均值 average, 计算中位数 median\n",
    "'''通用函数：\n",
    "numpy.sqrt(array) 平方根函数\n",
    "numpy.exp(array) e^array[i]的数组\n",
    "numpy.abs/fabs(array) 计算绝对值\n",
    "numpy.square(array)\n",
    "计算各元素的平方 等于array 2\n",
    "numpy.log/log10/log2(array)\n",
    "计算各元素的各种对数\n",
    "numpy.sign(array) 计算各元素正负号\n",
    "numpy.isnan(array) 计算各元素是否为NaN\n",
    "numpy.isinf(array) 计算各元素是否为NaN\n",
    "numpy.cos/cosh/sin/sinh/tan/tanh(array) 三角函数\n",
    "numpy.modf(array) 将 array 中值得整数和小数分离，作两个数组返回\n",
    "numpy.ceil(array) 向上取整,也就是取比这个数大的整数\n",
    "numpy.floor(array) 向下取整,也就是取比这个数小的整数\n",
    "numpy.rint(array) 四舍五入\n",
    "numpy.trunc(array) 向 0 取整\n",
    "numpy.cos(array) 正弦值\n",
    "numpy.sin(array) 余弦值\n",
    "numpy.tan(array) 正切值\n",
    "numpy.add(array1,array2) 元素级加法\n",
    "numpy.subtract(array1,array2) 元素级减法\n",
    "numpy.multiply(array1,array2) 元素级乘法\n",
    "numpy.divide(array1,array2)\n",
    "元素级除法 array1./array2\n",
    "numpy.power(array1,array2) 元素级指数 array1.^array2\n",
    "numpy.maximum/minimum(array1,aray2) 元素级最大值\n",
    "numpy.fmax/fmin(array1,array2) 元素级最大值，忽略 NaN\n",
    "numpy.mod(array1,array2) 元素级求模\n",
    "numpy.copysign(array1,array2) 将第二个数组中值得符号复制给第一个数组中值\n",
    "numpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)\n",
    "元素级比较运算，产生布尔数组\n",
    "numpy.logical_end/logical_or/logic_xor(array1,array2)元素级的真值逻辑运算\n",
    "'''"
   ],
   "id": "dbb18c58d40234be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[88 82 81]\n",
      "75\n",
      "[75 81]\n",
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[1 2 3 4 5]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n",
      "[2 1] [75 81]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'通用函数：\\nnumpy.sqrt(array) 平方根函数\\nnumpy.exp(array) e^array[i]的数组\\nnumpy.abs/fabs(array) 计算绝对值\\nnumpy.square(array)\\n计算各元素的平方 等于array 2\\nnumpy.log/log10/log2(array)\\n计算各元素的各种对数\\nnumpy.sign(array) 计算各元素正负号\\nnumpy.isnan(array) 计算各元素是否为NaN\\nnumpy.isinf(array) 计算各元素是否为NaN\\nnumpy.cos/cosh/sin/sinh/tan/tanh(array) 三角函数\\nnumpy.modf(array) 将 array 中值得整数和小数分离，作两个数组返回\\nnumpy.ceil(array) 向上取整,也就是取比这个数大的整数\\nnumpy.floor(array) 向下取整,也就是取比这个数小的整数\\nnumpy.rint(array) 四舍五入\\nnumpy.trunc(array) 向 0 取整\\nnumpy.cos(array) 正弦值\\nnumpy.sin(array) 余弦值\\nnumpy.tan(array) 正切值\\nnumpy.add(array1,array2) 元素级加法\\nnumpy.subtract(array1,array2) 元素级减法\\nnumpy.multiply(array1,array2) 元素级乘法\\nnumpy.divide(array1,array2)\\n元素级除法 array1./array2\\nnumpy.power(array1,array2) 元素级指数 array1.^array2\\nnumpy.maximum/minimum(array1,aray2) 元素级最大值\\nnumpy.fmax/fmin(array1,array2) 元素级最大值，忽略 NaN\\nnumpy.mod(array1,array2) 元素级求模\\nnumpy.copysign(array1,array2) 将第二个数组中值得符号复制给第一个数组中值\\nnumpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)\\n元素级比较运算，产生布尔数组\\nnumpy.logical_end/logical_or/logic_xor(array1,array2)元素级的真值逻辑运算\\n'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:17:27.086316Z",
     "start_time": "2025-02-14T07:17:27.027992Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(30).reshape(10, 3)\n",
    "np.mean(t, axis=0)\n"
   ],
   "id": "9688fa5aa7e59e9c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13.5, 14.5, 15.5])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 11 数组的拼接与分割",
   "id": "1c7d01dd4cce9a18"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:28:19.823801Z",
     "start_time": "2025-02-14T07:28:19.811008Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 有的时候我们需要将两个数据加起来一起研究分析，我们就可以将其进行拼接然后分析\n",
    "import numpy as np\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "c = np.array([[9, 10],[11,12]])\n",
    "# 要求a,b两个数组的维度相同\n",
    "# print ('沿轴 0 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis= 0))\n",
    "print ('-' * 50)\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis = 1))\n",
    "print ('-' * 50)\n",
    "print(np.stack((a, b, c), axis=0).shape)"
   ],
   "id": "11e83c1a0872a89f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "--------------------------------------------------\n",
      "沿轴 1 连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "--------------------------------------------------\n",
      "(3, 2, 2)\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:31:42.016285Z",
     "start_time": "2025-02-14T07:31:41.968947Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠\n",
    "print ('沿轴 0 连接两个数组：')\n",
    "print (np.stack((a,b),axis= 0))\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.stack((a,b),axis = 1))\n",
    "# 3. 矩阵垂直拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "[6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "[18,19,20,21,22,23]]\n",
    "result = np.vstack((v1,v2))\n",
    "print(result)\n",
    "# 4. 矩阵水平拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "[6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "[18,19,20,21,22,23]]\n",
    "result = np.hstack((v1,v2))\n",
    "print(result)"
   ],
   "id": "f694981b1bdb7755",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "[[[1 2]\n",
      "  [5 6]]\n",
      "\n",
      " [[3 4]\n",
      "  [7 8]]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数组的分割",
   "id": "67f7b221a14f6d20"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "'''\n",
    "参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置\n",
    "（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为0，横向切分。为1时，纵向切分\n",
    "'''\n",
    "import numpy as np\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "print ('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr,3)\n",
    "print (b)\n",
    "# 2.numpy.hsplit 函数用于水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "\n",
    "harr = np.floor(10*np.random.random((2, 6)))\n",
    "print ('原 array：')\n",
    "print(harr)\n",
    "print ('拆分后：')\n",
    "print(np.hsplit(harr, 3))\n",
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4,4)\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('竖直分割：')\n",
    "b = np.vsplit(a,2)\n",
    "print (b)"
   ],
   "id": "9ebdaa491b9fea06"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " 12 数组中nan和inf",
   "id": "c768ff3ba687aacc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:39:07.805554Z",
     "start_time": "2025-02-14T07:39:07.676396Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF，最小的负整数是 0x8000 0000。\n",
    "查阅资料后，发现 inf 表示无穷大，需要使用 float(‘inf’) 函数来转化，那么对应的就有\n",
    "float('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现inf呢？ 比如一个数字除以0，Python中会报错，但是numpy中会是一个\n",
    "inf\n",
    "或者-inf\n",
    "另外还有 nan，这种写法在 pandans 中常见，表示缺失的数据，所以一般用 nan 来表示。\n",
    "任何与其做运算结果都是 nan（比如零除以零，在numpy中）\n",
    "'''\n",
    "import numpy as np\n",
    "# nan是not a number的意思，表示缺失数据\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))\n",
    "#--判断数组中为nan的个数\n",
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "# 将三行四列的数改成nan\n",
    "t[3,4] = np.nan\n",
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 并 且 np.nan != np.nan 结果 是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.count_nonzero(t != t))\n",
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)\n",
    "#----------练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "\n",
    "# 将数组中的一部分替换nan\n",
    "t[1,3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n"
   ],
   "id": "e5a9f6b32bd0e0a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n",
      "23\n",
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:43:30.863059Z",
     "start_time": "2025-02-14T07:43:30.816205Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)\n"
   ],
   "id": "4954396edde458b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:44:42.335416Z",
     "start_time": "2025-02-14T07:44:42.324881Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t != t)\n",
    "print('-' * 50)\n",
    "print(np.count_nonzero(t != t)) # 统计nan的个数"
   ],
   "id": "91b215b4d9ba3fa2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "--------------------------------------------------\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T11:27:31.516913Z",
     "start_time": "2025-02-14T11:27:30.723955Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "3eb87bf4b5957f10",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T11:50:41.598054Z",
     "start_time": "2025-02-14T11:50:41.576821Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('------------------')\n",
    "print(t.shape)\n",
    "print(id(t))\n",
    "for i in range(t.shape[1]):\n",
    "    # 获取当前的列数据中是否含有nan\n",
    "    tem_col = t[:, i]\n",
    "    \n",
    "    # 判断当前的列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(tem_col != tem_col)\n",
    "    # 条件成立说明含nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一刻不为nan的数据拿出来，并计算平均值\n",
    "        tem_col_not_nan = tem_col[tem_col == tem_col]\n",
    "        print(tem_col_not_nan)\n",
    "        # 将nan替换成这一刻的平均值\n",
    "        tem_col[np.isnan(tem_col)] = np.mean(tem_col_not_nan)\n",
    "\n",
    "print(t)\n",
    "print(id(t))\n",
    "        "
   ],
   "id": "4372a0058af6d144",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "------------------\n",
      "(4, 6)\n",
      "2122394051824\n",
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "2122394051824\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "13 二维数组的转置，轴滚动",
   "id": "3719a30ab6d7d4ed"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T11:57:35.387110Z",
     "start_time": "2025-02-14T11:57:35.339440Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的轴滚动，swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "print(a.shape)\n",
    "np.rollaxis(a, 3, 1).shape # 三轴放一轴"
   ],
   "id": "1c9991c92a80d942",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5, 6)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(3, 6, 4, 5)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T11:58:57.191394Z",
     "start_time": "2025-02-14T11:58:57.146124Z"
    }
   },
   "cell_type": "code",
   "source": "np.rollaxis(a, 1, 3).shape # 一轴放三轴",
   "id": "dda11c3ca4a11039",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5, 4, 6)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T11:54:07.422818Z",
     "start_time": "2025-02-14T11:54:06.964213Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对换数组的维度\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a))\n",
    "# 与transpose 一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('转置数组：')\n",
    "print (a.T)\n",
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print (' 原 数 组 ：')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "print ('调用 swapaxes 函数后的数组：')\n",
    "print (re)\n",
    "t=np.rollaxis(t,3,0)\n",
    "print(t.shape)"
   ],
   "id": "58e3adad4a9e42ff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    },
    {
     "ename": "AxisError",
     "evalue": "axis 3 is out of bounds for array of dimension 2",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAxisError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[7], line 24\u001B[0m\n\u001B[0;32m     22\u001B[0m \u001B[38;5;28mprint\u001B[39m (\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m调用 swapaxes 函数后的数组：\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m     23\u001B[0m \u001B[38;5;28mprint\u001B[39m (re)\n\u001B[1;32m---> 24\u001B[0m t\u001B[38;5;241m=\u001B[39m\u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrollaxis\u001B[49m\u001B[43m(\u001B[49m\u001B[43mt\u001B[49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m     25\u001B[0m \u001B[38;5;28mprint\u001B[39m(t\u001B[38;5;241m.\u001B[39mshape)\n",
      "File \u001B[1;32mD:\\python3.10\\lib\\site-packages\\numpy\\core\\numeric.py:1313\u001B[0m, in \u001B[0;36mrollaxis\u001B[1;34m(a, axis, start)\u001B[0m\n\u001B[0;32m   1242\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m   1243\u001B[0m \u001B[38;5;124;03mRoll the specified axis backwards, until it lies in a given position.\u001B[39;00m\n\u001B[0;32m   1244\u001B[0m \n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1310\u001B[0m \n\u001B[0;32m   1311\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m   1312\u001B[0m n \u001B[38;5;241m=\u001B[39m a\u001B[38;5;241m.\u001B[39mndim\n\u001B[1;32m-> 1313\u001B[0m axis \u001B[38;5;241m=\u001B[39m \u001B[43mnormalize_axis_index\u001B[49m\u001B[43m(\u001B[49m\u001B[43maxis\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mn\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   1314\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m start \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m0\u001B[39m:\n\u001B[0;32m   1315\u001B[0m     start \u001B[38;5;241m+\u001B[39m\u001B[38;5;241m=\u001B[39m n\n",
      "\u001B[1;31mAxisError\u001B[0m: axis 3 is out of bounds for array of dimension 2"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T12:06:26.525803Z",
     "start_time": "2025-02-14T12:06:26.469257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据拷贝，copy()和赋值的区别\n",
    "b = np.array([[1, 2, 3],[1, 2, 3]])\n",
    "a = b.copy()\n",
    "a\n"
   ],
   "id": "89a71397c9b432e1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "a2148676f8d1185f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T12:06:54.832217Z",
     "start_time": "2025-02-14T12:06:54.805036Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b[0, 0] = 3\n",
    "print(b)\n",
    "a"
   ],
   "id": "262909699c788822",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 3]\n",
      " [1 2 3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T12:07:38.904239Z",
     "start_time": "2025-02-14T12:07:38.889916Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.rand(2, 3, 4)\n",
    "print(arr)"
   ],
   "id": "f5d4b77298eb2c3a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.64248935 0.43991571 0.19856292 0.7596702 ]\n",
      "  [0.78800129 0.94659262 0.35447201 0.65212368]\n",
      "  [0.10087628 0.66421244 0.41144263 0.42560469]]\n",
      "\n",
      " [[0.06273397 0.87918297 0.35813355 0.752579  ]\n",
      "  [0.79172859 0.60176716 0.28248941 0.59830904]\n",
      "  [0.75144345 0.6938981  0.22621356 0.93201968]]]\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
